//
//  CityWeather.swift
//  ZhiYunWeather
//
//  Created by Wade on 2024/7/9.
//

import Foundation
import UIKit
import ZFPlayer
import LGSideMenuController
class KEAlphabet : KYBaseVC ,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource{
    var aBtn:UIButton?
    var content:String?
    var selectIndex:Int = 0
    lazy var menuList:Array<KEAlphabetWords> = {
        /*
         var letter:String?
         var words:String?
         var image:String?
         var audio:String?
         */
        
        return [KEAlphabetWords(dictionary: [
            "letter":"A",
            "letter_image":"A-pic",
            "words":"ant",
            "words_zh":"蚂蚁",
            "image":"ant-pic",
            "audio_zh":"ant-audio-zh",
            "audio_en":"ant-audio-en",
            "sentence_en" : [
                "I find an ant."
            ],"sentence_zh" : [
                "我发现了一只蚂蚁。"
            ],
        ]),KEAlphabetWords(dictionary: [
            "letter":"A",
            "letter_image":"A-pic",
            "words":"apple",
            "words_zh":"苹果",
            "image":"apple-pic",
            "audio_zh":"apple-audio-zh",
            "audio_en":"apple-audio-en",
            "sentence_en" : [
                "I hava an apple."
            ],"sentence_zh" : [
                "我有一个苹果。"
            ],
        ])]
    }()
    override func viewDidLoad() {
        self.view.backgroundColor = .MainBGColor
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        setupSubViews()
        self.audioPlayer = ZFAudioPlayer()
        self.collectionView?.register(KEAlphabetCollectionCell.self, forCellWithReuseIdentifier: "KEAlphabetCollectionCell")
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.menuList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KEAlphabetCollectionCell", for: indexPath) as! KEAlphabetCollectionCell
        cell.words = self.menuList[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let words = self.menuList[indexPath.item]
        guard let audioPath = Bundle.main.path(forResource: words.audio_en, ofType:"mp3") else {
            print("aBtnClick 找不到音频文件:"+self.content!)
           return
        }
        let audioURL = URL.init(fileURLWithPath: audioPath)
        self.audioPlayer?.playLocalAudio(audioURL)
        self.selectIndex = indexPath.row
        self.tableView?.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let words = self.menuList[self.selectIndex]
        if(words.sentence_en != nil){
            return words.sentence_en!.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let words = self.menuList[self.selectIndex]
        cell.textLabel?.text = words.sentence_en![indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    }
    
}
